<template>
	<view class="wrap">
		<view class="modItem">
			<view class="modTit"><text class="must">*</text>选择学科</view>
			<view class="selectWrap hasFlex">
				<picker class="pickerView" @change="bindSubject" :value="subIndex" :range="subject" range-key="subjectName">
					<view class="payTypeTxt">{{subject[subIndex].subjectName}}</view>
				</picker>
				<view class="iconfont icon-sanjiao"></view>
			</view>
		</view>
		<view class="modItem">
			<view class="modTit spaceBet"><view><text class="must">*</text>标题</view><view class="clear" @tap="clearTit">清空</view></view>
			<textarea class="clsNameInp" v-model="bbsTit" placeholder="请输入标题(80字内)" maxlength="80" auto-height />
		</view>
		<view class="modItem">
			<view class="modTit spaceBet"><view><text class="must">*</text>帖子内容</view><view class="clear" @tap="clearCon">清空</view></view>
			<textarea class="queConInp" v-model="bbsCon" placeholder="请输入帖子内容(500字内)" maxlength="500" />
		</view>
		<button class="comBtn hasBotFixed" :loading="btnDisabFlag" :disabled="btnDisabFlag" @tap="pubBbs">{{btnTit}}</button> 
	</view>
</template>

<script>
	import { pubBbs,getSelfSubject } from '@/api/zhuxue'
	export default {
		data() {
			return {
				subject:[{}],
				subIndex:0,
				subId:0,
				bbsTit:'',
				bbsCon:'',
				btnDisabFlag:false,
				btnTit:'发帖',
				hasPubFlag:false,
				gradeNo:0
			}
		},
		onLoad(options){
			this.gradeNo = options.gradeNo;
			this.loadSelfSubject();
		},
		onShow(){
			var pubBbsField = uni.getStorageSync('pubBbsField');
			if(pubBbsField){
				this.bbsTit = pubBbsField.bbsTit;
				this.bbsCon = pubBbsField.bbsCon;
			}
		},
		onUnload(){
			if(this.bbsTit != '' && !this.hasPubFlag){
				var field = { bbsTit:this.bbsTit,bbsCon:this.bbsCon }
				uni.setStorageSync('pubBbsField',field);
			}
		},
		methods: { 
			loadSelfSubject(){
				let field = { account:uni.getStorageSync('token') }
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				getSelfSubject({content:contentStr}).then(res=>{
					this.subject = res.data.datas.subjectList;
					this.subId = this.subject[0].subjectId;
				}).catch(err=>{
					console.log(err)
				});
			},
			pubBbs(){
				if(this.subId == 0){
					this.util.showToast('请选择学科');
				}else if(this.bbsTit == ''){
					this.util.showToast('帖子标题不能为空');
				}else if(this.bbsCon == ''){
					this.util.showToast('帖子内容不能为空');
				}else{
					var field = { account:uni.getStorageSync('token'),title:this.bbsTit,con:this.util.replaceToBr( this.bbsCon ),subId:this.subId,gread:this.gradeNo };
					let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
					this.btnDisabFlag = true;
					this.btnTit = '发布中...';
					pubBbs({content:contentStr}).then(res=>{
						this.util.showToastSuc('发布成功');
						let pages = getCurrentPages();
						let prevPages = pages[pages.length-2];
						prevPages.$vm.isRefreshFlag = true;
						this.hasPubFlag = true;
						uni.removeStorageSync('pubBbsField');
						setTimeout(()=>{
							uni.navigateBack({delta:1})
						},1500);
					}).catch(err=>{
						this.btnTit = '发帖';
						this.btnDisabFlag = false;
						console.log(err)
					});
				}
			},
			bindSubject(e){
				this.subIndex = e.detail.value;
				this.subId = this.subject[e.detail.value].subjectId;
			},
			clearTit(){
				this.bbsTit = '';
				if(this.bbsCon == ''){
					uni.removeStorageSync('pubBbsField');
				}
			},
			clearCon(){
				this.bbsCon = '';
				if(this.bbsTit == ''){
					uni.removeStorageSync('pubBbsField');
				}
			}
		}
	}
</script>

<style lang="scss">
	.comBtn{
		border-radius: 0;
		background: $pss-color-blue;
		@include respTo(pad){
			height: 60rpx;
			line-height: 60rpx;
			font-size: $padSize18;
		}
	}
	.uni-uploader-title{
		color: $pss-color-red;
	}
	.modItem{
		margin-bottom: 20rpx;
	}
	.selectWrap{
		background: $pss-bg-gray;
		border-radius: 10rpx;
		align-items: center;
		@include respTo(phone){
			height: 80rpx;
		}
		@include respTo(pad){
			height: 60rpx;
		}
		.pickerView{
			width: 100%;
			height: 100%;
			text-indent: 30rpx;
			color: $pss-text-color3;
			@include respTo(phone){
				line-height: 80rpx;
				font-size: $font-size30;
			}
			@include respTo(pad){
				line-height: 60rpx;
				font-size: $padSize16;
			}
		}
		.icon-sanjiao{
			margin-right: 10rpx;
			font-size: 36rpx;
			color: $pss-text-colora;
		}
	}
	.clsNameInp,
	.queConInp{
		width: 96%;
		background: $pss-bg-gray;
		color: $pss-text-color3;
		border-radius: 10rpx;
		padding: 24rpx 2%;
		@include respTo(phone){
			font-size: $font-size30;
		}
		@include respTo(pad){
			font-size: $padSize16;
		}
	}
	.queConInp{
		height: 500rpx;
	}
	.modTit{
		color: $pss-text-color3;
		font-weight: bold;
		@include respTo(phone){
			height: 90rpx;
			line-height: 90rpx;
			font-size: $font-size32;
		}
		@include respTo(pad){
			height: 60rpx;
			line-height: 60rpx;
			font-size: $padSize18;
		}
		.must{
			color: $pss-color-red;
			margin-right: 10rpx;
			opacity: .6;
		}
		.clear{
			color: $pss-color-blue;
			height: 100%;
			padding-left: 50rpx;
			font-weight: normal;
			@include respTo(phone){
				font-size:$font-size28;
			}
			@include respTo(pad){
				font-size: $padSize14;
			}
		}
	}
	.wrap{
		@include respTo(phone){
			padding: 10rpx 30rpx 130rpx;
		}
		@include respTo(pad){
			padding: 10rpx 20rpx 70rpx;
		}
	}
</style>
